/*=========================================================================

  Library:   CFS

  Copyright (c) Kitware Inc.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0.txt

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

=========================================================================*/

#include "CFSErrorLogAbstractMessageHandler.h"

// Qt includes
#include <QHash>
#include <QDateTime>

// CFS includes
#include "CFSErrorLogContext.h"

// --------------------------------------------------------------------------
// CFSErrorLogAbstractMessageHandlerPrivate

// --------------------------------------------------------------------------
class CFSErrorLogAbstractMessageHandlerPrivate
{
public:
  CFSErrorLogAbstractMessageHandlerPrivate();
  ~CFSErrorLogAbstractMessageHandlerPrivate();

  bool                        Enabled;
  QString                     HandlerPrettyName;

  // Use "int" instead of "CFSErrorLogModel::TerminalOutput" to avoid compilation warning ...
  // qhash.h:879: warning: passing 'CFSErrorLogModel::TerminalOutput' chooses 'int' over 'uint' [-Wsign-promo]
  QHash<int, CFSErrorLogTerminalOutput*> TerminalOutputs;
};

// --------------------------------------------------------------------------
CFSErrorLogAbstractMessageHandlerPrivate::
CFSErrorLogAbstractMessageHandlerPrivate()
  : Enabled(false)
{
}

// --------------------------------------------------------------------------
CFSErrorLogAbstractMessageHandlerPrivate::~CFSErrorLogAbstractMessageHandlerPrivate()
{
}

// --------------------------------------------------------------------------
// CFSErrorLogAbstractMessageHandlerPrivate methods

// --------------------------------------------------------------------------
CFSErrorLogAbstractMessageHandler::CFSErrorLogAbstractMessageHandler()
  : Superclass(), d_ptr(new CFSErrorLogAbstractMessageHandlerPrivate)
{
}

// --------------------------------------------------------------------------
CFSErrorLogAbstractMessageHandler::~CFSErrorLogAbstractMessageHandler()
{
}

// --------------------------------------------------------------------------
QString CFSErrorLogAbstractMessageHandler::handlerPrettyName()const
{
  Q_D(const CFSErrorLogAbstractMessageHandler);
  if (d->HandlerPrettyName.isEmpty())
    {
    return this->handlerName();
    }
  else
    {
    return d->HandlerPrettyName;
    }
}

// --------------------------------------------------------------------------
void CFSErrorLogAbstractMessageHandler::setHandlerPrettyName(const QString& newHandlerPrettyName)
{
  Q_D(CFSErrorLogAbstractMessageHandler);
  d->HandlerPrettyName = newHandlerPrettyName;
}

// --------------------------------------------------------------------------
bool CFSErrorLogAbstractMessageHandler::enabled()const
{
  Q_D(const CFSErrorLogAbstractMessageHandler);
  return d->Enabled;
}

// --------------------------------------------------------------------------
void CFSErrorLogAbstractMessageHandler::setEnabled(bool value)
{
  Q_D(CFSErrorLogAbstractMessageHandler);
  if (value == d->Enabled)
    {
    return;
    }
  this->setEnabledInternal(value);
  d->Enabled = value;
}

// --------------------------------------------------------------------------
void CFSErrorLogAbstractMessageHandler::handleMessage(const QString& threadId,
                                                      CFSErrorLogLevel::LogLevel logLevel,
                                                      const QString& origin,
                                                      const CFSErrorLogContext& logContext,
                                                      const QString &text)
{
  Q_D(CFSErrorLogAbstractMessageHandler);
  if (logLevel <= CFSErrorLogLevel::Info)
    {
    if(d->TerminalOutputs.contains(CFSErrorLogTerminalOutput::StandardOutput))
      {
      d->TerminalOutputs.value(CFSErrorLogTerminalOutput::StandardOutput)->output(text);
      }
    }
  else
    {
    if(d->TerminalOutputs.contains(CFSErrorLogTerminalOutput::StandardError))
      {
      d->TerminalOutputs.value(CFSErrorLogTerminalOutput::StandardError)->output(text);
      }
    }
  emit this->messageHandled(QDateTime::currentDateTime(), threadId, logLevel, origin, logContext, text);
}

// --------------------------------------------------------------------------
CFSErrorLogTerminalOutput* CFSErrorLogAbstractMessageHandler::terminalOutput(
    CFSErrorLogTerminalOutput::TerminalOutput terminalOutputType)const
{
  Q_D(const CFSErrorLogAbstractMessageHandler);
  if(d->TerminalOutputs.contains(terminalOutputType))
    {
    return d->TerminalOutputs.value(terminalOutputType);
    }
  return 0;
}

// --------------------------------------------------------------------------
void CFSErrorLogAbstractMessageHandler::setTerminalOutput(
    CFSErrorLogTerminalOutput::TerminalOutput terminalOutputType, CFSErrorLogTerminalOutput* terminalOutput)
{
  Q_D(CFSErrorLogAbstractMessageHandler);
  d->TerminalOutputs.insert(terminalOutputType, terminalOutput);
}
